<template>
  <div>
    <!-- 顶部 -->
    <div class="Top">
      <div class="left">
        <img src="@/assets/images/logo.png" class="logImg" />
        <span>OCAP</span>
      </div>
    </div>

    <div class="content">
      <!-- 登陆框 -->
      <div class="login">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <h3 class="title">登陆</h3>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon
                slot="prefix"
                icon-class="user"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="prefix"
                icon-class="password"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <el-checkbox
            v-model="loginForm.rememberMe"
            style="margin: 0px 0px 25px 0px"
            >记住密码</el-checkbox
          >
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <!-- 登录，加载中 -->
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <!-- 注册 -->
            <!-- <div style="float: right" v-if="register">
              <router-link class="link-type" :to="'/register'"
                >立即注册</router-link
              >
            </div> -->
          </el-form-item>
        </el-form>
        <!--  底部  -->
        <div class="el-login-footer">
          <span></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getRouters } from "@/api/menu";
import { getToken } from "@/utils/auth";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        // username: "admin",
        // password: "admin123",
        username: "",
        password: "",
        rememberMe: false,
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
      },
      loading: false,
      // 验证码开关
      // captchaEnabled: true,
      // isPassing: false,
      // 注册开关
      // register: false,
      redirect: undefined,
    };
  },
  // components: { dragVerify },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCookie();
  },
  mounted() {
    //   this.handleLogin();
  },
  computed: {},
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.content {
  // height: 850px;
  position: absolute;
  top: 50px;
  bottom: 0;
  left: 0;
  right: 0;
  background-image: url("../assets/images/login-background.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

.Top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 50px;
  // background-color: rgb(4, 71, 124);
  background: linear-gradient(to right, rgb(6, 19, 51), rgb(16, 73, 144));
  color: #fff;
  height: 50px;
  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    margin-right: 20px;
    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }
    .langImg-container {
      .langImg {
        cursor: pointer;
        width: 30px;
        height: 32px;
        border-radius: 10px;
        background-color: #fff;
        border-radius: 15px;
      }
      .langImg-wrapper {
        margin-top: 10px;
        position: relative;

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -10px;
          top: 25px;
          font-size: 12px;
          color: #fff;
        }
        .lang {
          font-size: 8px;
          color: #fff;
        }
      }
    }
  }
  .logImg {
    height: 32px;
    margin: 10px 6px;
    vertical-align: middle;
    background-color: #fff;
    border-radius: 15px;
  }
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    margin-right: 20px;
    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }
    .langImg-container {
      .langImg {
        cursor: pointer;
        width: 30px;
        height: 32px;
        border-radius: 10px;
        background-color: #fff;
        border-radius: 15px;
      }
      .langImg-wrapper {
        margin-top: 10px;
        position: relative;

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -10px;
          top: 25px;
          font-size: 12px;
          color: #fff;
        }
        .lang {
          font-size: 8px;
          color: #fff;
        }
      }
    }
  }
  .logImg {
    height: 32px;
    margin: 10px 6px;
    vertical-align: middle;
    background-color: #fff;
    border-radius: 15px;
  }
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-size: 24px;
  font-weight: 700;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.el-login-footer {
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
